// index.js
Page({
  data: {
    content: '',
    list: [],
    lastId: 0,
    showLayer: false,
    showLayer1: false,
    animationData: {},
    animationData1: {},
    socketOpen: false,  // 新增变量以跟踪WebSocket连接状态
  },

  message: '',
  onLoad: function() {
    this.animation = wx.createAnimation({
      duration: 300,
      timingFunction: 'ease',
    });
    this.animation1 = wx.createAnimation({
      duration: 300,
      timingFunction: 'ease',
    });
    this.initWebSocket();
  },
  
  input: function (e) {
    this.message = e.detail.value
  },

  showOptions: function() {
    this.animation.bottom(0).step();
    this.setData({
      showLayer: true,
      animationData: this.animation.export(),
    });
  },
  closeLayer: function() {
    this.animation.bottom('-100%').step();
    this.setData({
      showLayer: false,
    });
  },
  showOptions1: function() {
    this.animation1.bottom(0).step();
    this.setData({
      showLayer1: true,
      animationData1: this.animation1.export(),
    });
  },
  option2: function() {
    wx.showToast({
      title: '按钮被点击了',
      icon: 'success',
      duration: 2000
    });
  },
  option1: function() {
    wx.showToast({
      title: '按钮被点击了',
      icon: 'success',
      duration: 2000
    });
  },
  closeLayer1st: function() {
    this.closeLayer();  // 重用closeLayer函数
  },
  closeLayer2nd: function() {
    this.animation1.bottom('-100%').step();
    this.setData({
      showLayer1: false,
    });
  },

  initWebSocket: function() {  
    if (!this.data.socketOpen) {  
      const socketOpenCallback = (res) => {  
        console.log('WebSocket 连接已打开！');  
        this.setData({ socketOpen: true });  
      };  
      const socketErrorCallback = (err) => {  
        console.error('WebSocket 连接打开失败，请检查！', err);  
      };  
      const socketMessageCallback = (msg) => {  
        console.log('收到服务器消息：', msg.data);  
        // 解析消息并添加到聊天界面  
        const parsedMessage = JSON.parse(msg.data);  
        this.data.messages.push(parsedMessage);  
        this.setData({ messages: this.data.messages });  
      };  
  
      const socketTask = wx.connectSocket({  
        url: 'http://api.qingyunke.com/api.php', // 你的WebSocket服务器地址  
      });  
  
      socketTask.onOpen(socketOpenCallback);  
      socketTask.onError(socketErrorCallback);  
      socketTask.onMessage(socketMessageCallback);  
  
      // 注意：你还需要添加 onClose 和其他必要的回调  
    }  
  },  
  
  // 关闭WebSocket连接
  onUnload: function () {
    
  },

  // 点击“发送”按钮时将message中的内容发送到服务器
  send: function () {
    // 判断发送内容是否为空
    if (!this.message) {
      wx.showToast({
        title: '消息不能为空',
        icon: 'none',
        duration: 2000
      })
      return
    }
    const list = this.data.list
    const lastId = list.length
    list.push({
      id: lastId,
      content: this.message,
      role: 'me'
    })
    this.setData({
      list,
      lastId,
      content: ''
    }) 
  wx.request({
    url: 'http://api.qingyunke.com/api.php?key=free&appid=0&msg='+this.message,
    method:'GET',
    success:res=>{
      console.log(res.data.content)
      this.onreceive(res.data.content)
    },
    fail:failres=>{
      console.log(failres)
    }
  })
  },
  onreceive(message){
    var message=message
    var list=this.data.list
    var lastId=list.length
    list.push({
      id:lastId,
      content:message,
      role:'server'
    })
    this.setData({
      list,
      lastId
    })
    },
})
